import {
  GET_COURSE_DETAIL,
  GET_COURSE_LIST,
  GET_LESSON_DETAIL,
  GET_LESSON_LIST,
  SET_CURRENT_COURSE
} from "../../mutation-types";

export default {
  [GET_COURSE_LIST](state, { type, list = [], current_page, total_pages }) {
    list.forEach(item => {
      item.width = item.progress;
    });
    const data = {
      list: type === 1 ? list.reverse() : list,
      current_page,
      total_pages
    };
    type === 1 ? (state.todayCourse = data) : (state.myCourse = data);
  },

  [GET_COURSE_DETAIL](state, data) {
    state.courseDetail = data;
  },

  [GET_LESSON_LIST](state, data) {
    const base = [
      {
        img: "bg_study@2x.png",
        c_img: "ico_study@2x.png",
        url: "/pages_course/course_learn/course_learn",
        path: "/pages_course/course_learn/course_learn",
        text: "学一学",
        title: "学一学",
        type_id: 1
      },
      {
        img: "bg_read@2x.png",
        c_img: "ico_read@2x.png",
        url: "/pages_course/course_read/course_read",
        path: "/pages_course/course_read/course_read",
        text: "读一读",
        title: "读一读",
        type_id: 2
      },
      {
        img: "bg_practise@2x.png",
        c_img: "ico_practise@2x.png",
        url: "/pages_course/course_practice/course_practice",
        path: "/pages_course/course_practice/course_practice",
        text: "练一练",
        title: "练一练",
        type_id: 3
      },
      {
        img: "bg_learn@2x.png",
        c_img: "ico_learn@2x.png",
        url: "/pages_course/course_use/course_use",
        path: "/pages_course/course_use/course_use",
        text: "学以致用",
        title: "学以致用",
        type_id: 4
      },
      {
        img: "ft2/bg_learn%20copy@2x.png",
        c_img: "ft2/ico_learn@2x.png",
        url: "/pages_course/course_upload/index",
        path: "/pages_course/course_upload/index",
        text: "传作品",
        title: "传作品",
        type_id: 5
      }
    ];

    data.lessons.forEach((item, index) => {
      base.forEach(rest => {
        if (item.name === rest.text) {
          data.lessons[index] = { ...item, ...rest };
        }
      });
    });
    state.lessonList = data;
  },

  [GET_LESSON_DETAIL](state, { lessonDetail, lessonType, type_id, id }) {
    state.lessonDetail = lessonDetail;
    state.lessonType = lessonType;
  },

  [SET_CURRENT_COURSE](state, { id, type_id }) {
    state.id = id;
    state.type_id = type_id;
  }
};
